package com.xyoto.community.service.impl;

import com.xyoto.community.constant.DeleteStatus;
import com.xyoto.community.constant.DynamicOperateType;
import com.xyoto.community.constant.PersonalStatus;
import com.xyoto.community.constant.TopicUserHotType;
import com.xyoto.community.dto.SaveDynamicForwardDTO;
import com.xyoto.community.entity.main.Dynamic;
import com.xyoto.community.entity.main.DynamicForward;
import com.xyoto.community.enums.DynamicEnum;
import com.xyoto.community.exception.ExceptionUtils;
import com.xyoto.community.mapper.main.DynamicForwardMapper;
import com.xyoto.community.mapper.main.DynamicMapper;
import com.xyoto.community.service.DynamicCacheService;
import com.xyoto.community.service.DynamicForwardService;
import com.xyoto.community.service.TopicHotDynamicCacheService;
import com.xyoto.community.service.TopicUserHotService;
import com.xyoto.community.utils.JwtUtils;
import com.xyoto.community.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 动态转发实现类
 */
@Service
@Slf4j
public class DynamicForwardServiceImpl implements DynamicForwardService {

    @Autowired
    private DynamicForwardMapper dynamicForwardMapper;

    @Resource
    private HttpServletRequest request;

    @Autowired
    private DynamicMapper dynamicMapper;

    @Autowired
    private TopicUserHotService topicUserHotService;

    @Autowired
    private TopicHotDynamicCacheService topicHotDynamicCacheService;

    @Autowired
    private DynamicCacheService dynamicCacheService;

    /**
     * 新增动态转发
     *
     * @param dynamicForwardDTO
     * @return
     */
    @Override
    public Result saveForwardDynamic(SaveDynamicForwardDTO dynamicForwardDTO) {

        // 获取用户id
        Integer userId = JwtUtils.getMemberIdByJwtToken(request);

        //判断是否存在该动态
        Dynamic dynamic = dynamicMapper.getDynamicById(dynamicForwardDTO.getDynamicId());
        if (dynamic == null || dynamic.getDeleteStatus() == DeleteStatus.YES) {
            ExceptionUtils.error(DynamicEnum.DYNAMIC_NOT_EXIST);
        }

        //存在则执行动态转发功能
        log.info("正在执行动态转发功能");
        DynamicForward dynamicForward = new DynamicForward();
        dynamicForward.setDynamicId(dynamicForwardDTO.getDynamicId());
        dynamicForward.setUserId(userId);
        // 查询是否已经转发过
        DynamicForward getRepeatForward = dynamicForwardMapper.getRepeatForward(dynamicForward);

        // 是否+1
        boolean state = false;

        if (getRepeatForward == null) {
            //第一次转发
            log.info("正在执行动态转发SQL");
            Integer saveDynamicForward = dynamicForwardMapper.saveDynamicForward(dynamicForward);
            if (saveDynamicForward == null || saveDynamicForward == 0) {
                ExceptionUtils.error(DynamicEnum.DYNAMIC_FORWARD_ADD_FAILED);
            }
            state = true;

            // 给用户增加活跃度
            topicUserHotService.addHot(userId, dynamic.getTopicId(),TopicUserHotType.FORWARD);

            //判断该动态是否为私密,给话题下的热门动态加分
            if (Short.valueOf(PersonalStatus.VISIBLE_TO_ALL).equals(dynamic.getPersonal()) && dynamic.getTopicId() != null){
                //非私密给该动态加分
                topicHotDynamicCacheService.addTopicHotDynamicScore(dynamic.getTopicId(),dynamic.getId(), DynamicOperateType.DYNAMIC_FORWARD);
            }

            // 将动态缓存到redis
            dynamicCacheService.saveDynamic2Cache(dynamic,DynamicOperateType.DYNAMIC_FORWARD);

            // 更新缓存中的动态转发数
            dynamicCacheService.updateDynamicForwardCountInCache(dynamicForward);
        }

        Map<String, Boolean> data = new HashMap<>();
        data.put("state", state);
        return Result.success(data);
    }
}
